package com.jnu.keepaccounts;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.jnu.keepaccounts.data.Data;
import com.jnu.keepaccounts.data.DataBank;
import com.jnu.keepaccounts.data.DataTiaoMu;
import com.jnu.keepaccounts.data.DataZhuanZhang;

import java.lang.reflect.AccessibleObject;
import java.util.ArrayList;

public class AccountManage extends AppCompatActivity {
    private ArrayList<Data> datas ;
    private double net_assets;
    private double net_assets_weixin=0;
    private double net_assets_zhifubao=0;
    private double net_assets_xianjin=0;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accountmanage);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
        ImageView imageView = findViewById(R.id.back);
        imageView.setOnClickListener(v -> finish());

        DataBank dataBank = new DataBank(getApplicationContext());

        this.datas = dataBank.loadData();
        ArrayList<DataZhuanZhang> dataZhuanZhangs = dataBank.loadZData();

        for(Data data:datas){
            if(data.getFlag()== 1){
                if((((DataTiaoMu)data).getLaiYuan()).equals("微信") ){
                    net_assets_weixin += ((DataTiaoMu)data).getShuE();
                }
                else if((((DataTiaoMu)data).getLaiYuan()).equals("现金")){
                    net_assets_xianjin += ((DataTiaoMu)data).getShuE();
                }
                else if((((DataTiaoMu)data).getLaiYuan()).equals("支付宝")){
                    net_assets_zhifubao += ((DataTiaoMu)data).getShuE();
                }
                net_assets += ((DataTiaoMu)data).getShuE();
            }
        }
        for(DataZhuanZhang dataZhuanZhang:dataZhuanZhangs){
            if(dataZhuanZhang.getIn().equals("微信"))net_assets_weixin+=dataZhuanZhang.getMoney();
            else if(dataZhuanZhang.getIn().equals("现金"))net_assets_xianjin+=dataZhuanZhang.getMoney();
            else if(dataZhuanZhang.getIn().equals("支付宝"))net_assets_zhifubao+=dataZhuanZhang.getMoney();

            if(dataZhuanZhang.getOut().equals("微信"))net_assets_weixin-=dataZhuanZhang.getMoney();
            else if (dataZhuanZhang.getOut().equals("现金"))net_assets_xianjin-=dataZhuanZhang.getMoney();
            else if (dataZhuanZhang.getOut().equals("支付宝"))net_assets_zhifubao-=dataZhuanZhang.getMoney();
        }
        TextView xianjin = findViewById(R.id.yue_xianjin);
        TextView weixin  = findViewById(R.id.yue_weixin);
        TextView zhifubao = findViewById(R.id.yue_zhifubao);
        TextView total = findViewById(R.id.total);

        total.setText(net_assets+"");
        xianjin.setText(net_assets_xianjin+"");
        weixin.setText(net_assets_weixin+"");
        zhifubao.setText(net_assets_zhifubao+"");

        LinearLayout xianjin_tiao = findViewById(R.id.xianjin);
        LinearLayout weixin_tiao = findViewById(R.id.weixin);
        LinearLayout zhifubao_tiao = findViewById(R.id.zhifubao);

        xianjin_tiao.setOnClickListener(v -> {
            Intent intent = new Intent(this, AccountSee.class);
            intent.putExtra("name","现金");
            intent.putExtra("money",net_assets_xianjin);
            startActivity(intent);
        });

        weixin_tiao.setOnClickListener(v -> {
            Intent intent = new Intent(this, AccountSee.class);
            intent.putExtra("name","微信");
            intent.putExtra("money",net_assets_weixin);
            startActivity(intent);
        });

        zhifubao_tiao.setOnClickListener(v -> {
            Intent intent = new Intent(this, AccountSee.class);
            intent.putExtra("name","支付宝");
            intent.putExtra("money",net_assets_zhifubao);
            startActivity(intent);
        });
    }
}
